package dao

import (
	"fmt"
	"github.com/astaxie/beego/orm"
	"analysis-ss/models"
)

func QueryCenter(centerId string, start, length int) ([]models.Center, int64, error) {
	var centers []models.Center
	o := orm.NewOrm()
	o.Using("default")
	sqlStr := `select
	          distinct(center_id) as center_id,
	          center_name as center_name
	          from
	          t_schedule where 1=1
	           `
	if centerId != "" && centerId != "0" {
		sqlStr = fmt.Sprint(sqlStr, ` and  center_id =`, centerId)
	}

	sqlStr = fmt.Sprint(sqlStr, ` order by ( center_id + 1 )  limit `, start, ",", length)
	num, err := o.Raw(sqlStr).QueryRows(&centers)
	return centers, num, err
}

func QueryTeacher(centerId string, teacherName interface{}, start, length int) ([]models.Center, int64, error) {
	var centers []models.Center
	o := orm.NewOrm()
	o.Using("default")
	sqlStr := `select
                   distinct(teacher_id),
                   teacher_name,
                   center_id,
                   center_name
                   from
                   t_schedule where 1=1
	           `

	if centerId != "" && centerId != "0" {
		sqlStr = fmt.Sprint(sqlStr, ` and  center_id =`, centerId)
	}
	if teacherName != "" {
		sqlStr = fmt.Sprint(sqlStr, ` and  teacher_name like "%`, teacherName, `%" `)
	}

	sqlStr = fmt.Sprint(sqlStr, ` order by center_id, teacher_id limit `, start, ",", length)
	num, err := o.Raw(sqlStr).QueryRows(&centers)
	return centers, num, err
}

// 根据中心总页码
func QueryCenterTotal(centerId interface{}) (int, error) {
	var Total int
	o := orm.NewOrm()
	o.Using("default")
	sqlStr := `select
                  count(distinct(center_id))
                  from
                  t_schedule
                  where 1=1 `
	if centerId != "" && centerId != "0" {
		sqlStr = fmt.Sprint(sqlStr, ` and  center_id =`, centerId)
	}

	err := o.Raw(sqlStr).QueryRow(&Total)
	return Total, err
}

// 根据老师页面的总页码
func QueryTeacherTotal(centerId string, teacherName interface{}) (int, error) {
	var Total int
	o := orm.NewOrm()
	o.Using("default")
	sqlStr := `select
	           count(distinct(teacher_id))
                  from
                  t_schedule
                  where 1=1 `
	if centerId != "" && centerId != "0" {
		sqlStr = fmt.Sprint(sqlStr, ` and  center_id =`, centerId)
	}
	if teacherName != "" {
		sqlStr = fmt.Sprint(sqlStr, ` and  teacher_name like "%`, teacherName, `%" `)
	}

   beego.info("aaa");
	err := o.Raw(sqlStr).QueryRow(&Total)
	return Total, err
}
